package com.xyoto.community.service.impl;

import com.xyoto.community.cache.UserCache;
import com.xyoto.community.constant.OssBucketModuleName;
import com.xyoto.community.dto.UploadPictureDTO;
import com.xyoto.community.dto.UserSaveTitleDTO;
import com.xyoto.community.entity.main.TitleLogo;
import com.xyoto.community.entity.main.TitleName;
import com.xyoto.community.entity.main.UserTitle;
import com.xyoto.community.enums.FileEnum;
import com.xyoto.community.enums.UserEnum;
import com.xyoto.community.exception.ExceptionUtils;
import com.xyoto.community.mapper.main.TitleLogoMapper;
import com.xyoto.community.mapper.main.TitleNameMapper;
import com.xyoto.community.mapper.main.UserMapper;
import com.xyoto.community.mapper.main.UserTitleMapper;
import com.xyoto.community.po.UserTitlePO;
import com.xyoto.community.service.PictureService;
import com.xyoto.community.service.UserTitleService;
import com.xyoto.community.utils.RedisUtils;
import com.xyoto.community.utils.Result;
import com.xyoto.community.utils.XYOtoOss;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 用户头衔实现类
 */
@Service
public class UserTitleServiceImpl implements UserTitleService {

    @Autowired
    private UserTitleMapper userTitleMapper;

    @Autowired
    private PictureService pictureService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private XYOtoOss xyOtoOss;

    @Autowired
    private TitleNameMapper titleNameMapper;

    @Autowired
    private TitleLogoMapper titleLogoMapper;

    /**
     * 新增头衔
     * @param multipartFile
     * @return
     */
    @Transactional
    @Override
    public Result saveTitle(MultipartFile multipartFile) {

        //TODO 校验管理员

        // 检查图片合法性
        UploadPictureDTO uploadPictureDTO = new UploadPictureDTO();
        uploadPictureDTO.setMultipartFile(multipartFile);
        pictureService.checkPicture(uploadPictureDTO);

        String path = null;

        // 上传图片到oss
        try {
            path = xyOtoOss.uploadFileAvatars(new MultipartFile[]{multipartFile}, OssBucketModuleName.PRIVATE_LETTER);
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (path == null){
            ExceptionUtils.error(FileEnum.FILE_UPLOAD_FAIL);
        }

        UserTitle userTitle = new UserTitle();
        userTitle.setUserTitlePath(path);

        Integer saveTitle = userTitleMapper.saveTitle(userTitle);
        if (saveTitle == null || saveTitle == 0){
            ExceptionUtils.error(UserEnum.TITLE_SAVE_ERROR);
        }

        return Result.success();
    }

    /**
     * 删除头衔
     * @param id
     * @return
     */
    @Override
    public Result removeTitleById(Integer id) {
        //校验id
        if (id < 0){
            ExceptionUtils.error(UserEnum.TITLE_NOT_EXIST);
        }
        //TODO 校验管理员

        Integer removeTitle = userTitleMapper.removeTitleById(id);
        if (removeTitle == null || removeTitle == 0){
            ExceptionUtils.error(UserEnum.TITLE_REMOVE_ERROR);
        }

        return Result.success();
    }

    /**
     * 用户新增头衔
     * @param userSaveTitleDTO
     * @return
     */
    @Override
    public Result userSaveTitle(UserSaveTitleDTO userSaveTitleDTO) {

        //TODO 校验管理员

        Integer userId = userSaveTitleDTO.getUserId();
        Integer userTitleId = userSaveTitleDTO.getUserTitleId();

        //校验是否存在此头衔
        String title = userTitleMapper.getTitle(userTitleId);
        if (title == null || "".equals(title)){
            ExceptionUtils.error(UserEnum.TITLE_NOT_EXIST);
        }

        //校验用户是否存在
        UserCache redisUserMessage = redisUtils.getRedisUserMessage(userId);
        if (redisUserMessage == null) {
            ExceptionUtils.error(UserEnum.USER_INFO_NOT_EXIST);
        }

        Integer saveUserTitle = userMapper.updateUserTitle(userSaveTitleDTO);
        if (saveUserTitle == null || saveUserTitle == 0){
            ExceptionUtils.error(UserEnum.USER_TITLE_SAVE_ERROR);
        }

        return Result.success();
    }

    /**
     * 根据头衔名称id和头衔logo的id获取头衔名称和头衔logo
     * @author 廖振辉
     * @param titleNameId 头衔名称id
     * @param titleLogoId 头衔logo的id
     * @return {@link UserTitlePO}
     */
    @Override
    public UserTitlePO getUserTitlePO(Integer titleNameId, Integer titleLogoId) {
        UserTitlePO userTitlePO = new UserTitlePO();
        if (titleNameId != null){
            // 获取头衔名称
            TitleName titleName = titleNameMapper.selectOneById(titleNameId);
            if (titleName != null){
                userTitlePO.setTitleName(titleName.getTitleName());
            }
        }
        if (titleLogoId != null){
            // 获取头衔logo
            TitleLogo titleLogo = titleLogoMapper.selectOneById(titleLogoId);
            if (titleLogo != null){
                userTitlePO.setTitleLogoPath(titleLogo.getTitleLogoPath());
            }
        }
        return userTitlePO;
    }
}
